﻿// -----------------------------------------------------------------------
// <copyright file="GooglePlusButtonHelper.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SDW_GooglePlusButton
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;

    /// <summary>
    /// Helper class to generate code for Google +1 button
    /// </summary>
    public static class GooglePlusButtonHelper
    {
        /// <summary>
        /// Generates all code for a Google +1 button
        /// </summary>
        /// <param name="html">The htmlhelper to extend with this method </param>
        /// <param name="parameters">The parameters for the button</param>
        /// <returns>Html code for a Google +1 button</returns>
        public static MvcHtmlString GooglePlusButton(this HtmlHelper html, GooglePlusParameters parameters)
        {
            string code = GenerateScriptTag(html, parameters).ToHtmlString();
            code += GenerateButtonTag(html, parameters).ToHtmlString();
            if (parameters.ParseTags == GooglePlusButtonParseTags.Explicit)
            {
                code += GenerateLoadScriptButton(html, true).ToHtmlString();
            }

            return MvcHtmlString.Create(code);
        }

        /// <summary>
        /// Generates the script tag for a Google +1 button
        /// </summary>
        /// <param name="html">The htmlhelper to extend with this method </param>
        /// <param name="parameters">The parameters for the button</param>
        /// <returns>Html code for a script tag</returns>
        public static MvcHtmlString GenerateScriptTag(this HtmlHelper html, GooglePlusParameters parameters)
        {
            string jsonFormat = "{0}:'{1}'";
            StringBuilder sb = new StringBuilder();
            TagBuilder scriptTag = new TagBuilder("script");
            IDictionary<string, string> scriptTagParameters = new Dictionary<string, string>();
            scriptTagParameters.Add("type", "text/javascript");
            scriptTagParameters.Add("src", "https://apis.google.com/js/plusone.js");
            scriptTag.MergeAttributes(scriptTagParameters);
            sb.Append("{");
            if (parameters.Language.HasValue)
            {
                sb.AppendFormat(jsonFormat, "lang", parameters.Language.GetStringValue());
            }

            sb.Append(",");
            if (parameters.ParseTags.HasValue)
            {
                sb.AppendFormat(jsonFormat, "parsetags", parameters.ParseTags.ToString().ToLower());
            }

            sb.Append("}");
            scriptTag.InnerHtml = sb.ToString();
            return MvcHtmlString.Create(scriptTag.ToString());
        }

        /// <summary>
        /// /// Generates code for the Google +1 button
        /// </summary>
        /// <param name="html">The htmlhelper to extend with this method</param>
        /// <param name="parameters">The parameters for the button</param>
        /// <returns>Html code for a Google +1 button</returns>
        public static MvcHtmlString GenerateButtonTag(this HtmlHelper html, GooglePlusParameters parameters)
        {
            TagBuilder buttontag = new TagBuilder("g:plusone");

            if (parameters.Size.HasValue)
            {
                buttontag.MergeAttribute("size", parameters.Size.ToString().ToLower());
            }

            if (parameters.Count.HasValue)
            {
                buttontag.MergeAttribute("count", parameters.Count.ToString().ToLower());
            }

            if (!string.IsNullOrEmpty(parameters.Url))
            {
                buttontag.MergeAttribute("href", parameters.Url.ToString());
            }

            if (!string.IsNullOrEmpty(parameters.CallBack))
            {
                buttontag.MergeAttribute("callback", parameters.CallBack);
            }

            return MvcHtmlString.Create(buttontag.ToString());
        }

        /// <summary>
        /// /// Generates code for the loading tag for a explicit loading Google +1 button
        /// </summary>
        /// <param name="html">The htmlhelper to extend with this method</param>
        /// <param name="withScriptTag">If true the code while generate a script tag If false it generates javascript code only</param>
        /// <returns>Html code for a loading script tag</returns>
        public static MvcHtmlString GenerateLoadScriptButton(this HtmlHelper html, bool withScriptTag)
        {
            if (withScriptTag)
            {
                TagBuilder scriptTag = new TagBuilder("script");
                scriptTag.MergeAttribute("type", "text/javascript");
                scriptTag.InnerHtml = "gapi.plusone.go();";
                return MvcHtmlString.Create(scriptTag.ToString());
            }

            return MvcHtmlString.Create("gapi.plusone.go();");
        }
    }
}
